//
//  FenXiangXiangQingViewController.m
//  XiangMu
//
//  Created by 康世标 on 15/8/25.
//  Copyright (c) 2015年 康世标. All rights reserved.
//

#import "FenXiangXiangQingViewController.h"
#import "IQKeyboardManager.h"
@interface FenXiangXiangQingViewController ()

@end

@implementation FenXiangXiangQingViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"分享详情";
    
    [IQKeyboardManager sharedManager];

    [self.view1.layer setBorderWidth:.3];
    [self.tableView registerNib:[UINib nibWithNibName:@"FenXiangXiangQiuTableViewCell" bundle:nil] forCellReuseIdentifier:@"fenxiangxiangqing"];
     [self.tableView registerNib:[UINib nibWithNibName:@"PingLunCellTableViewCell" bundle:nil] forCellReuseIdentifier:@"pinglun"];
    self.zanLabel.text = [NSString stringWithFormat:@"%@",[self.datasArr valueForKey:@"clickCount"]];
    self.pingLuLable.text = [NSString stringWithFormat:@"%@",[self.datasArr valueForKey:@"commentCount"]];
    self.tableView.backgroundColor = CL_efeff4;
    self.tableView.separatorColor = CL_efeff4;
    [self QingQiu];
    
    [self mbProgress];
    
 
    [self dibuPingLun];
    [self kaiChi];
}

//弹出来的输入框
- (void)dibuPingLun{
    vi = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenH-40-49-20, kScreenW, 50)];
    vi.backgroundColor = [UIColor whiteColor];
    
    textFi = [[UITextField alloc]initWithFrame:CGRectMake(10, 5, kScreenW-60, 40)];
    textFi.placeholder = @"我想说。。。";
    textFi.font = [UIFont systemFontOfSize:15];
    textFi.borderStyle = UITextBorderStyleRoundedRect;

    textFi.delegate = self;
    [vi addSubview:textFi];
    
    UIButton *butt = [UIButton buttonWithType:UIButtonTypeCustom];
    butt.frame = CGRectMake(textFi.frame.origin.x+textFi.frame.size.width+5, 5, 40, 30);
    [butt setTitle:@"发送" forState:UIControlStateNormal];
    [butt setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [butt addTarget:self action:@selector(commentsClick) forControlEvents:UIControlEventTouchUpInside];
    [vi addSubview:butt];
    [self.view addSubview:vi];
    vi.hidden = YES;

}
- (void)kaiChi{
    
    if (self.longVc) {
        vi.hidden = NO;
        [textFi becomeFirstResponder];
    }
}
- (void)mbProgress{
    
    hud= [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    hud.labelText = @"数据加载中...";
    
    
}
- (void)QingQiu{
    
    self.dic = [[NSMutableDictionary alloc]initWithCapacity:0];
    
    [self.dic setObject:@([[self.datasArr valueForKey:@"id"] longLongValue]) forKey:@"shareId"];
    [self.dic setObject:@(1) forKey:@"page"];
    [self.dic setObject:@(10000) forKey:@"rows"];
    
    [QingQiuShuJu PingLu:self.dic HuiChuan:^(NSMutableArray *arr, BOOL isOK) {
        
        if (isOK) {
            
            if ([[arr valueForKey:@"status"] intValue]==200) {
                
                [hud hide:YES];
                hud.removeFromSuperViewOnHide = YES;
                
                self.pingLuArr = [arr valueForKey:@"data"];
                
                [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
                
            }
            else{
//                hud.mode = MBProgressHUDModeText;
//                hud.labelText = [arr valueForKey:@"msg"];
//                [hud hide:YES afterDelay:2];
                 [hud hide:YES];
                hud.removeFromSuperViewOnHide = YES;
            }
            
            
        }else{
         
            hud.mode = MBProgressHUDModeText;
            hud.labelText = @"网路连接错误";
            [hud hide:YES afterDelay:2];
            hud.removeFromSuperViewOnHide = YES;
        }
    }];
}
#pragma mark---UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==1) {
        
        CGRect rect = [[[self.pingLuArr objectAtIndex:indexPath.row] valueForKey:@"content"] boundingRectWithSize:CGSizeMake(kScreenW-58-8, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :[UIFont systemFontOfSize:14]} context:nil];
        
        return 60+rect.size.height+5;
    }
    CGFloat imageWith = self.imagesArr.count *200+self.imagesArr.count *10;
    
    CGRect rect = [[self.datasArr valueForKey:@"info"] boundingRectWithSize:CGSizeMake(kScreenW-8, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :[UIFont systemFontOfSize:14]} context:nil];
    
    
    return 100+imageWith+rect.size.height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section==1) {
        
        return 30;
    }
    
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section==1) {
        
        UIView * vc = nil;
        
        vc = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        vc.backgroundColor = CL_e6e6e6;
        
        UILabel * lable = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 30)];
        lable.text = @"用户评论";
        
        [vc addSubview:lable];
        
        
        return vc;
    }
    return nil;
}
#pragma makr---UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section==1) {
        
        return self.pingLuArr.count;
    }
    
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        FenXiangXiangQiuTableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"fenxiangxiangqing" forIndexPath:indexPath];
        
        [cell ImgageView:self.imagesArr Datas:self.datasArr];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    else{
        
        PingLunCellTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"pinglun"];
        [cell PingLunDic:[self.pingLuArr objectAtIndex:indexPath.row]];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }

    
    
    
}
- (IBAction)Zan:(id)sender{
    if ([UserManager  login])
    {
        
        [[[UIAlertView alloc]initWithTitle:nil message:@"您还没有登陆" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil]show];
        
        return;
    }
    
    hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    hud.margin =20;
    
    
  
    
    [QingQiuShuJu FenXiangDianZan:[[NSUserDefaults standardUserDefaults] objectForKey:@"ID"] sheareId:[ self.datasArr valueForKey:@"id"] Blokc:^(NSDictionary *dic, BOOL isOK1)
     {
         NSLog(@"--%@",dic);
         
         if (isOK1)
         {
             
             
             if ([[dic valueForKey:@"status"] intValue]==200)
             {
                 
              NSInteger inte = [[self.datasArr valueForKey:@"clickCount"] integerValue];
                 
              inte ++;                 
               
                 
                 [self.datasArr setObject:@(inte) forKey:@"clickCount"];
               self.zanLabel.text = [NSString stringWithFormat:@"%@",[self.datasArr valueForKey:@"clickCount"]];
                
                 [[NSNotificationCenter defaultCenter]postNotificationName:@"jiajia" object:self];
                 
                 
             [hud hide:YES];
                 
                 
                 
             }
             else{
                 NSInteger inte = [[self.datasArr valueForKey:@"clickCount"] integerValue];
                 
                 inte --;

                 [self.datasArr setObject:@(inte) forKey:@"clickCount"];
                 self.zanLabel.text = [NSString stringWithFormat:@"%@",[self.datasArr valueForKey:@"clickCount"]];
   
                 [hud hide:YES];
                 
                [[NSNotificationCenter defaultCenter]postNotificationName:@"jianjian" object:self];
                 
             }
             [self.tableView reloadData];
             
             
             
         }
         else{
             
             hud.mode = MBProgressHUDModeText;
             hud.labelText = @"网络连接失败";
             
             [hud hide:YES afterDelay:2];
             
         }
         
         
         
     }];
    
    hud.removeFromSuperViewOnHide = YES;

}
//评论
- (IBAction)PingLu:(id)sender{
    if ([UserManager  login])
    {
        
        [[[UIAlertView alloc]initWithTitle:nil message:@"您还没有登陆" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil]show];
        
        return;
    }
  
    [textFi becomeFirstResponder];


    vi.hidden= NO;
}

#pragma mark ---UIAlertViewDalegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 0) {
        
        DengLuViewController * dl = [[DengLuViewController alloc]init];
        
        UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:dl];
        
        [self presentViewController:nav animated:YES completion:nil];
        
    }
    
}
//分享
- (IBAction)FenXing:(id)sender{
    KSActionSheet * sheet = [[KSActionSheet alloc]initWithActionSheet];
    sheet.delegate = self;
    [sheet show];
    

}
- (void)commentsClick{
    
    vi.hidden = YES;

    [textFi resignFirstResponder];
    
    hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"数据加载中...";
    if ([textFi.text isEqualToString:@""]||textFi==nil) {
        
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"请输入内容";
        [hud hide:YES afterDelay:1];
      
        
    }
    else{
        
        NSString * userId= [[NSUserDefaults standardUserDefaults]objectForKey:@"ID"];
        NSString *sherId = [self.datasArr valueForKey:@"id"];
        
        
        
        [QingQiuShuJu addPingLu:@{@"userId":userId,@"shareId":sherId,@"content":textFi.text} Block:^(NSDictionary *dic, BOOL isOK)
        {
            
            if ([[dic valueForKey:@"status"] intValue]==200) {
                
                
                [self QingQiu];
                hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
                
                
                hud.mode = MBProgressHUDModeCustomView;
                hud.labelText = @"发布成功";
                
                [hud show:YES];
                [hud hide:YES afterDelay:2];
            }
            else{
                
                hud.mode = MBProgressHUDModeText;
                hud.labelText = [dic valueForKey:@"msg"];
                
                [hud hide:YES afterDelay:1];
               
            }
        }];
    }
  hud.removeFromSuperViewOnHide= YES;
    
}
#pragma mark -- KSActionSheetDelegate
- (void)alertView:(KSActionSheet *)alertView clickedButtonAtIndexPantex:(NSInteger)buttonIndex{
    
        if (buttonIndex ==0) {
            
            return;
        }
    
    NSInteger Id = [[self.datasArr valueForKey:@"id"] integerValue];
    
        
        WXMediaMessage *message = [WXMediaMessage message];
        message.title = @"爱陪护";
        message.description = @"爱陪护";
        [message setThumbImage:[UIImage imageNamed:@"Image_Icon"]];
        
        WXWebpageObject *ext = [WXWebpageObject object];
    ext.webpageUrl =[NSString stringWithFormat:@"%@%@?shareId=%ld",URL,URL_FENXING,(long)Id];
    
        message.mediaObject = ext;
        
        SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
        req.bText = NO;
        req.message = message;
        req.scene = (int)buttonIndex -1;
        
        BOOL isok=   [WXApi sendReq:req];
        
        if (!isok) {
            [[[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"未安装微信客户端" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil] show];
            
        }
        

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textFi resignFirstResponder];
    vi.hidden = YES;
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
